/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.retroactive;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.BaseController;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.services.manage.offline.bean.Offline;
import shop.services.manage.offline.impl.OfflineServiceImpl;
import shop.services.manage.offlineAccount.OfflineAccountService;
import shop.services.manage.offlineAccount.bean.OfflineAccount;
import shop.services.manage.organize.OrganizeService;
import shop.services.manage.organize.bean.Organize;
import shop.services.manage.retroactive.RetroactiveService;
import shop.services.manage.retroactive.bean.Retroactive;

import javax.servlet.http.HttpServletRequest;

/**   
 * @类名称：RetroactiveAction      
 * @创建人：Ltz   
 * @创建时间：2017-08-12 上午10:39:07      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/retroactive/")
public class RetroactiveAction extends BaseController<Retroactive> {
    private static final Logger logger = LoggerFactory.getLogger(RetroactiveAction.class);
    @Autowired
    private RetroactiveService retroactiveService;
    @Autowired
    private OrganizeService organizeService;
    @Autowired
    OfflineServiceImpl offlineService;
    @Autowired
    private OfflineAccountService offlineAccountService;
    private static final String page_toList = "/manage/organize/resignApprove";
    public RetroactiveService getService() {
        return retroactiveService;
    }

    private RetroactiveAction(){
        super.page_toList = page_toList;
        super.page_toAdd = null;
        super.page_toEdit = "/manage/organize/resignApprove";
    }

    @RequestMapping("toOrganizeResignApproveList")
    public String toOrganizeResignApproveList(){
        return "/manage/organize/organizeResignApproveList";
    }
    @RequestMapping("list")
    @ResponseBody
    public PagerModel list(HttpServletRequest request, Organize organize) {
        int offset = 0;
        int pageSize = 10;
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if (pageSize < 0) {
            pageSize = 10;
        }
        shop.services.front.organize.bean.Organize   organize1=(shop.services.front.organize.bean.Organize) request.getSession().getAttribute("organizeInfo");
        organize.setId(organize1.getId());
        organize.setOffset(offset);
        organize.setPageSize(pageSize);
        PagerModel pager =new PagerModel();
        pager.setList(organizeService.list(organize));
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    @RequestMapping("toView")
    public String toView(@ModelAttribute("e") Retroactive e, ModelMap model) throws Exception {
        e = getService().selectOne(e);
        model.addAttribute("e", e);
        return "/manage/organize/reSignApproveView";
    }

    @Override
    public JSONResult updateJson(HttpServletRequest request, @ModelAttribute("e") Retroactive retroactive) throws Exception {
        Retroactive retroactive1=new Retroactive();
        retroactive1.setId(retroactive.getId());
         retroactive1 = retroactiveService.selectOne(retroactive1);
        Offline offline = new Offline();
        OfflineAccount offlineAccount = new OfflineAccount();
        offlineAccount.setAccountID(retroactive1.getUserId());
        offlineAccount.setOfflineID(retroactive1.getOfflineId());
        offlineAccount = offlineAccountService.selectOne(offlineAccount);
        offline.setId(retroactive1.getOfflineId());
        offline = offlineService.selectOne(offline);
        offline.setCardRecord((offline.getCardRecord() + 1));
        if (retroactive1.getSignType().equals("out"))
            offlineAccount.setSignOutTime(offline.getEndTime()+" "+offline.getEndTimeFrame());
        if (retroactive1.getSignType().equals("in"))
            offlineAccount.setSignOutTime(offline.getBeginTime()+" "+offline.getBeginTimeFrame());
        return super.updateJson(request, retroactive);
    }
}
